package com.video.web.controller.app;

import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.core.page.TableDataInfo;
import com.video.common.exception.ServiceException;
import com.video.common.utils.StringUtils;
import com.video.common.utils.bean.BeanUtils;
import com.video.video.domain.DBanner;
import com.video.video.domain.DClassify;
import com.video.video.domain.DSystemSettings;
import com.video.video.domain.app.dto.DClassifyDto;
import com.video.video.domain.app.dto.DGoodsDto;
import com.video.video.domain.app.dto.DGroupingDto;
import com.video.video.domain.app.dto.GooodFrontQuery;
import com.video.video.service.IDBannerService;
import com.video.video.service.IDClassifyService;
import com.video.video.service.IDGoodsService;
import com.video.video.service.IDGroupingService;
import com.video.video.service.IDSystemSettingsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "首页")
@RestController
@RequestMapping("/apph/home")
public class AppHomeController extends BaseController {
  @Resource
  private IDSystemSettingsService idSystemSettingsService;
  @Resource
  private IDBannerService idBannerService;
  @Resource
  private IDClassifyService idClassifyService;
  @Resource
  private IDGroupingService idGroupingService;
  @Autowired
  private IDGoodsService idGoodsService;

  @ApiOperation(value = "查询参数设置")
  @GetMapping("/config")
  public AjaxResult config() {
    DSystemSettings dConfig = new DSystemSettings();
    List<DSystemSettings> list = idSystemSettingsService.selectDSystemSettingsList(dConfig);
    if (list.size() != 1) {
      throw new ServiceException("参数设置有误，请联系客服", 10013);
    }
    return AjaxResult.success(list.get(0));
  }

  @ApiOperation(value = "查询轮播图片")
  @GetMapping("/banner")
  public AjaxResult banner() {
    DBanner banner = new DBanner();
    List<DBanner> list = idBannerService.selectDBannerList(banner);
    return AjaxResult.success(list);
  }

  @ApiOperation(value = "查询一级分类")
  @GetMapping("/classify")
  public AjaxResult classify() {
    DClassify classify = new DClassify();
    classify.setLevel("1");
    classify.setStatus("1");
    List<DClassify> list = idClassifyService.selectDClassifyList(classify);
    return AjaxResult.success(list);
  }

  @ApiOperation(value = "根据一级分类查询二级分类，未售出的")
  @GetMapping("/getClassifyByPid")
  public AjaxResult getClassifyByPid(String pid) {
    if(StringUtils.isEmpty(pid)){
      return AjaxResult.error("无效的一级分类");
    }
    List<DClassify> list = idClassifyService.selectDClassLevel2List(pid,"1");
    return AjaxResult.success(list);
  }


  @ApiOperation(value = "查询两级级分类")
  @GetMapping("/getTwoclassify")
  public AjaxResult getTwoclassify() {
    DClassify classify = new DClassify();
    classify.setLevel("1");
    classify.setStatus("1");
    List<DClassifyDto> list = idClassifyService.selectDClassifyDtoList(classify);

    for (DClassifyDto dto : list) {
      DClassify classify2 = new DClassify();
      classify2.setPid(dto.getId());
      classify2.setLevel("2");
      classify2.setStatus("1");
      List<DClassifyDto> list2 = idClassifyService.selectDClassifyDtoList(classify2);
      dto.setChildList(list2);
    }
    return AjaxResult.success(list);
  }

  @ApiOperation(value = "查询分组商品-根据会员等级计算佣金")
  @GetMapping("/getGroupGoods")
  public AjaxResult getGroupGoods() {
    String userId = String.valueOf(getLoginUser().getUserId());
    DClassify classify = new DClassify();
    classify.setStatus("1");
    List<DGroupingDto> list = idGroupingService.selectDGroupingDtoList(userId);
    return AjaxResult.success(list);
  }

  @ApiOperation(value = "查询商品-搜索关键词或者分类（支持分页）")
  @GetMapping("/search")
  public TableDataInfo search(GooodFrontQuery query) {
    String userId = String.valueOf(getLoginUser().getUserId());
    query.setUserId(userId);
    startPage();
    List<DGoodsDto> list = idGoodsService.selectDGoodsDtoList(query);
    return getDataTable(list);
  }

  @ApiOperation(value = "查询商品详情-（购买计算价格）")
  @GetMapping("/getGoodsDetails")
  public AjaxResult getGoodsDetails(String goodId) {
    String userId = String.valueOf(getLoginUser().getUserId());
    DGoodsDto dto = idGoodsService.getGoodsDetails(userId,goodId);
    return AjaxResult.success(dto);
  }

}
